<template>
  <div class="edit">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品{{ type }}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加/编辑表单 -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="margin-top: 20px"
    >
      <el-form-item label="商品名称" style="width: 400px">
        <el-input v-model="form.goodsname"></el-input>
      </el-form-item>

      <el-form-item label="一级分类">
        <el-select v-model="form.first_cateid" placeholder="请选择分类">
          <el-option
            v-for="c in cateList"
            :key="c.id"
            :label="c.catename"
            :value="c.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select v-model="form.second_cateid" placeholder="请选择分类">
          <el-option
            v-for="c in sec_cateList"
            :key="c.id"
            :label="c.catename"
            :value="c.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="价格" style="width: 200px">
        <el-input type="number" v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="市场价格" style="width: 200px">
        <el-input type="number" v-model="form.market_price"></el-input>
      </el-form-item>

      <el-form-item label="图片" style="width: 400px">
        <el-upload
          action="#"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :auto-upload="false"
          :on-change="handlerChange"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>

      <el-form-item label="商品规格">
        <el-select
          v-model="form.specsid"
          placeholder="请选择规格"
          @change="specsChange(form.specsid)"
        >
          <el-option
            v-for="c in specsList"
            :key="c.id"
            :label="c.specsname"
            :value="c.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="规格属性">
        <el-select
          v-model="form.specsattr"
          placeholder="请选择规格属性"
          multiple
        >
          <el-option v-for="c in attrsList" :key="c" :label="c" :value="c">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
          active-color="green"
          inactive-color="red"
        ></el-switch>
      </el-form-item>

      <el-form-item label="商品描述">
        <div id="editor"></div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import E from "wangeditor";
export default {
  name: "edit",
  data() {
    return {
      type: "添加",
      form: {
        first_cateid: "", //一级分类id
        second_cateid: "", //二级分类id
        goodsname: "",
        price: "",
        market_price: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: "",
      },
      img: "", //分类图标
      cateList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      specsList: [],
      attrsList: [],
      imgList: [],
      editor: null,
    };
  },
  computed: {
    sec_cateList() {
      let arr = this.cateList.filter(
        (item) => item.id === this.form.first_cateid
      );
      console.log(arr);
      if (arr[0]) {
        return arr[0].children;
      } else {
        return [];
      }
    },
  },

  //   默认执行
  mounted() {
    let id = this.$route.params.id;
    if (id) {
      this.type = "编辑";
      this.getGoodsInfo(id);
    }
    this.getCateList();
    this.getSpecsList();

    // 初始化富文本编辑器
    this.editor = new E("#editor");
    this.editor.create();
    this.specsChange();
  },
  methods: {
    getGoodsInfo(id) {
      this.$axios
        .get("/api/goodsinfo", { params: { id } })
        .then((result) => {
          console.log("goods", result);
          this.form = result.data.list;
          this.editor.txt.html(result.data.list.description);
          this.form.specsid = "";
          this.form.specsattr = "";
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getCateList() {
      this.$axios
        .get("/api/catelist", { params: { istree: 1 } })
        .then((result) => {
          this.cateList = result.data.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSpecsList() {
      this.$axios
        .get("/api/specslist")
        .then((result) => {
          console.log(result);
          this.specsList = result.data.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    specsChange(id) {
      if (id) {
        this.$axios
          .get("/api/specsinfo", { params: { id } })
          .then((result) => {
            this.attrsList = result.data.list[0].attrs;
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    onSubmit() {
      //   获得form表单，然后进行验证
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //深拷贝
          let str = JSON.stringify(this.form);
          let data = JSON.parse(str);
          console.log(this.form.specsattr);
          // 获取商品描述数据
          data.description = this.editor.txt.html();

          let url = "/api/goodsadd";
          if (this.type == "编辑") {
            data.id = this.$route.params.id;
            url = "/api/goodsedit";
          }
          // 把要上传的数据转成formData
          let formData = new FormData();
          for (let key in data) {
            formData.append(key, data[key]);
          }
          formData.append("img", this.img);

          this.$axios
            .post(url, formData)
            .then((result) => {
              console.log(result);
              if (result.data.code == 200) {
                this.$router.push("/goods");
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit");
          return false;
        }
      });
    },
    handlerChange(file) {
      // console.log(file);
      this.img = file.raw;
    },
  },
};
</script>

<style>
#editor {
  width: 100%;
  height: 500px;
}
</style>